/******************************************************************************
 Copyright (c) 2008-2024 Qualcomm Technologies International, Ltd.
 All Rights Reserved.
 Qualcomm Technologies International, Ltd. Confidential and Proprietary.

******************************************************************************/

#include "csr_synergy.h"
#include "csr_bt_cm_main.h"
#include "csr_bt_cm_dm.h"
#include "csr_bt_cm_util.h"

void CsrBtCmDmHciWritePageToCompleteHandler(cmInstanceData_t * cmData)
{
    DM_HCI_WRITE_PAGE_TIMEOUT_CFM_T * dmPrim;

    dmPrim = (DM_HCI_WRITE_PAGE_TIMEOUT_CFM_T *) cmData->recvMsgP;

    if (cmData->globalState == CSR_BT_CM_STATE_NOT_READY)
    {
        CmInitSequenceHandler(cmData,
                              CM_INIT_SEQ_WRITE_PAGE_TIMEOUT_CFM,
                              dmPrim->status,
                              CSR_BT_SUPPLIER_HCI);
    }
#ifdef CSR_BT_INSTALL_CM_WRITE_PAGE_TO
    else
    { /* The request came from the application */
        cmPendingMsg_t *pendingMsg;
        CsrUint8 type = CM_PENDING_MSG_WRITE_PAGE_TO_PARAMS;

        if (cmData->pendingMsgs == NULL)
        {
            CSR_LOG_TEXT_WARNING((CsrBtCmLto, 0, "Pending Queue Empty, Returning"));
            return;
        }

        pendingMsg = (cmPendingMsg_t *) CsrCmnListSearch((CsrCmnList_t *) cmData->pendingMsgs,
                                                         CmSearchPendingListByType,
                                                         (void *) &type);
        if (pendingMsg)
        {

            CsrBtCmWritePageToCfm *cfm = (CsrBtCmWritePageToCfm *)CsrPmemAlloc(sizeof(CsrBtCmWritePageToCfm));
            cfm->type                   = CSR_BT_CM_WRITE_PAGE_TO_CFM;

            if (dmPrim->status == HCI_SUCCESS)
            {
                cfm->resultCode         = CSR_BT_RESULT_CODE_CM_SUCCESS;
                cfm->resultSupplier     = CSR_BT_SUPPLIER_CM;
            }
            else
            {
                cfm->resultCode         = (CsrBtResultCode) dmPrim->status;
                cfm->resultSupplier     = CSR_BT_SUPPLIER_HCI;
            }
            CsrBtCmPutMessage(pendingMsg->arg.commonParams.appHandle, cfm);
            CsrPCmnListRemove(cmData->pendingMsgs, (CsrCmnListElm_t* ) pendingMsg);
        }
    }
#endif
}

#ifdef CSR_BT_INSTALL_CM_WRITE_PAGE_TO
void CsrBtCmDmWritePageToReqHandler(cmInstanceData_t * cmData)
{
    CsrBtCmWritePageToReq * cmPrim = (CsrBtCmWritePageToReq *) cmData->recvMsgP;
    cmPendingMsg_t *pendingMsg;

    CsrPCmnListAddLast(cmData->pendingMsgs, sizeof(cmPendingMsg_t), pendingMsg);
    pendingMsg->type = CM_PENDING_MSG_WRITE_PAGE_TO_PARAMS;
    pendingMsg->arg.commonParams.appHandle = cmPrim->appHandle;

    dm_hci_write_page_to(cmPrim->pageTimeout, NULL);
}
#endif
